#include "ekedatacache.h"

#include<qDebug>
ekeDataCache::ekeDataCache()
{

}

void ekeDataCache::setResult(const QString &caseName, ekeResult &result)
{
    _TestResult[caseName]=result;
}

void ekeDataCache::generateModel(QStandardItemModel *model)
{
    // 设置列数
    model->setColumnCount(7);

    // 创建列标题
    QStringList headers;
    headers<<"测试编号"<<"描述"<<"前置条件"<<"操作步骤"<<"期望结果"<<"延时函数"<<"结果";
    model->setHorizontalHeaderLabels(headers);


    // 设置行数，假设所有QVector的size相同
    //    int rowCount =_ekeCase._mapEKEcase.count();
    int rowCount =0;
    for (auto var = _sheetEkeCases.constBegin(); var !=_sheetEkeCases.constEnd(); ++var) {
        rowCount+= var.value()._mapEKEcase.count();
    }
    model->setRowCount(rowCount);

    int nRowindex =0;
    for (auto sheetCase = _sheetEkeCases.constBegin(); sheetCase !=_sheetEkeCases.constEnd(); ++sheetCase)
    {
        // 填充数据
        auto &ekedata = sheetCase.value()._mapEKEcase;

        foreach (auto& childeke,ekedata)
        {

            QStandardItem* item1 = new QStandardItem(childeke._childCaseNumber);
            model->setItem(nRowindex, 0, item1);
            QStandardItem* item2 = new QStandardItem(childeke._childCaseName);
            model->setItem(nRowindex, 1, item2);

            QString stringPreDiction =childeke._precondition.join(";");
            QStandardItem* item3 = new QStandardItem(stringPreDiction);
            model->setItem(nRowindex, 2, item3);

            QStringList listOperator;
            int nInputcount = childeke._inputs.count();
            qDebug()<<"inputcount"<<nInputcount<<endl;
            for (int i = 0; i < nInputcount; ++i)
            {
                if(childeke._inputs[i].first.isEmpty()||childeke._inputs[i].second.isEmpty())
                    continue;
                QString strKeyValue=childeke._inputs[i].first+"="+childeke._inputs[i].second;
                qDebug()<<strKeyValue<<endl;
                listOperator<<strKeyValue;
            }
            QStandardItem* item4 = new QStandardItem(listOperator.join(";"));
            model->setItem(nRowindex, 3, item4);

            QStringList listResult;
            int nOutputcount = childeke._output.count();
            qDebug()<<"nOutputcount"<<nOutputcount<<endl;
            for (int i = 0; i < nOutputcount; ++i)
            {
                if(childeke._output[i].first.isEmpty()||childeke._output[i].second.isEmpty())
                    continue;
                QString strKeyValue=childeke._output[i].first+"="+childeke._output[i].second;
                listResult<<strKeyValue;
            }
            QStandardItem* item5 = new QStandardItem(listResult.join(";"));
            model->setItem(nRowindex, 4, item5);

            //延时函数
            QStandardItem* itemDelayFunc = new QStandardItem(childeke._delayDescribe.join(";"));
            model->setItem(nRowindex, 5, itemDelayFunc);
            nRowindex++;
        }
    }
}

int ekeDataCache::getAllCaseCount()
{
    int count=0;
    for (auto var = _sheetEkeCases.constBegin(); var !=_sheetEkeCases.constEnd(); ++var) {
        count+= var.value()._mapEKEcase.count();
    }

    return count;
}

QVector<EKEChildCase> ekeDataCache::ToVector()
{
    QVector<EKEChildCase> serialVector;
    int count =getAllCaseCount();

    //声明缓存容量，在不超过设置量时 容器不会再次进行内存分配
    serialVector.reserve(count);

    for (auto sheet = _sheetEkeCases.constBegin(); sheet != _sheetEkeCases.constEnd(); ++sheet) {

        auto&childcases=sheet.value()._mapEKEcase;
        for (auto childcase = childcases.constBegin(); childcase !=childcases.constEnd(); ++childcase) {

            serialVector.append(childcase.value());
        }
    }

    return serialVector;
}

//根据唯一id:如 TC0_1获取对应的测试用例
EKEChildCase ekeDataCache::getChildCaseByCaseNumber(const QString &caseNumber)
{
    EKEChildCase childCase;
    for (auto sheet = _sheetEkeCases.constBegin(); sheet != _sheetEkeCases.constEnd(); ++sheet) {

        auto&childcases=sheet.value()._mapEKEcase;
        if(childcases.contains(caseNumber))
        {
            childCase= childcases[caseNumber];
        }
    }
    return childCase;
}

ekeResult ekeDataCache::getResultByTestCaseNumber(const QString &caseNumber)
{
    ekeResult result;
    if(_TestResult.contains(caseNumber))
    {
        result=_TestResult[caseNumber];
    }
    return result;
}
